package com.supconit.its.domaingenerator;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

import static com.supconit.its.environment.Environment.separator;

/**
 * <p>
 * 辅助生成代码模板（主要生成 domain ）
 * </p>
 *
 * @author 史金涛
 * @date 2020/8/8 10:34
 */
@Deprecated
public abstract class DomainTemplate {

    public final void domainGenerator() throws IOException {
        createDirs();
        createListDTO();
        createEntityDTO();
        createPageDTO();
        createAddBO();
        createEditBO();
        createEditDTO();
        createListQO();
        createEntityQO();
        createPageQO();
    }

    /**
     * createDirs
     */
    protected abstract void createDirs();

    /**
     * createEntityDto
     *
     * @throws IOException if create fail
     */
    protected abstract void createListDTO() throws IOException;

    /**
     * createEntityDTO
     *
     * @throws IOException if create fail
     */
    protected abstract void createEntityDTO() throws IOException;

    /**
     * createPageDto
     *
     * @throws IOException if create fail
     */
    protected abstract void createPageDTO() throws IOException;

    /**
     * createSaveBo
     *
     * @throws IOException if create fail
     */
    protected abstract void createAddBO() throws IOException;

    /**
     * createEditBo
     *
     * @throws IOException if create fail
     */
    protected abstract void createEditBO() throws IOException;

    /**
     * createEditDto
     *
     * @throws IOException if create fail
     */
    protected abstract void createEditDTO() throws IOException;

    /**
     * createListQo
     *
     * @throws IOException if create fail
     */
    protected abstract void createListQO() throws IOException;

    /**
     * createEntityQO
     *
     * @throws IOException if create fail
     */
    protected abstract void createEntityQO() throws IOException;

    /**
     * createPageQo
     *
     * @throws IOException if create fail
     */
    protected abstract void createPageQO() throws IOException;

    /**
     * mkdir if not exists
     *
     * @param abPath absolute path 绝对路径
     */
    final public void mkdir(String abPath) {
        String dirPath = abPath.substring(0, abPath.lastIndexOf(separator));
        File fileDir = new File(dirPath);
        if (!fileDir.exists()) {
            if (!fileDir.mkdirs()) {
                System.out.println(abPath + "目录创建失败");
            } else {
                System.out.println(abPath + "目录创建成功");
            }
        }
    }

    /**
     * 将lines写入文件中
     *
     * @param targetFilePath 目标文件绝对路径
     * @param lines          lines
     * @throws IOException if written exception
     */
    final public void writeLines(String targetFilePath, List<String> lines) throws IOException {
        if (ifFileNotExists(targetFilePath)) {
            RandomAccessFile rdw = new RandomAccessFile(targetFilePath, "rw");
            for (String line : lines) {
                rdw.writeBytes(line + "\r\n");
            }
            System.out.println(targetFilePath + "生成成功");
        }
    }

    /**
     * 判断文件是否存在
     *
     * @param abPath 绝对路径
     * @return 文件是否存在
     */
    final public boolean ifFileNotExists(String abPath) {
        File file = new File(abPath);
        if (file.exists()) {
            System.out.println(abPath + "已存在");
            return false;
        }
        return true;
    }

}
